package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jtoon.bean.Comics;
import com.jtoon.bean.Episode;
import com.jtoon.bean.Img;
import com.jtoon.util.PMF;

public class ImgManage extends HttpServlet {

	public static final int ADD = 1;
	public static final int UPDATE = 2;
	public static final int DELETE = 3;
	public static final int LIST = 4;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		long cid = 0;
		long eid = 0;
		int mode = 4;
		
		if(request.getParameter("cid") != null){
			cid = Long.valueOf(request.getParameter("cid"));
		}
		
		if(request.getParameter("eid") != null){
			eid = Long.valueOf(request.getParameter("eid"));
		}
		
		if(request.getParameter("mode") != null){
			mode = Integer.valueOf(request.getParameter("mode"));
		}
		
		switch (mode) {
			case ADD :	imgAdd(request, response, cid, eid); break;
			case UPDATE : imgUpdate(request, response, cid, eid); break; 
			case DELETE : imgDelete(request, response, cid, eid); break;
			case LIST : imgList(request, response, cid, eid); break;
		}
		
	}
	
	public void imgAdd(HttpServletRequest request, HttpServletResponse response, long cid, long eid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		String episodeTitle = "";
		Query query = pm.newQuery(Img.class);
		
		try{
			Comics comics = pm.getObjectById(Comics.class, cid);
			comicsTitle = comics.getTitle();
			
			Episode episode = pm.getObjectById(Episode.class, eid);
			episodeTitle = episode.getTitle();
		
			query.setOrdering("order desc");
			query.setFilter("eid == eidParam");
			query.declareParameters("Long eidParam");
			
			 
			String src = request.getParameter("src");
			int order = Integer.valueOf(request.getParameter("order"));
			
			Img img = new Img(src, order, eid);
			pm.makePersistent(img); 
			
			List<Img> imgList = (List<Img>)query.execute(eid);
			request.setAttribute("imgList", imgList);
			 
			RequestDispatcher rd = request.getRequestDispatcher("/manage/imgManage.jsp"); 
			request.setAttribute("cid", cid);
			request.setAttribute("eid", eid);
			request.setAttribute("comicsTitle", comicsTitle);
			request.setAttribute("episodeTitle", episodeTitle); 
			rd.forward(request, response);  
		}finally{
			query.closeAll(); 
			pm.close();
		}
		
		 	
	}
	
	public void imgUpdate(HttpServletRequest request, HttpServletResponse response, long cid, long eid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String episodeTitle = "";
		
		try{
			Episode episode = pm.getObjectById(Episode.class, eid);
			episodeTitle = episode.getTitle();
			
		}finally{
			 
		}
		
		RequestDispatcher rd = request.getRequestDispatcher("/manage/imgManage.jsp"); 
		request.setAttribute("cid", cid);
		request.setAttribute("eid", eid);
		request.setAttribute("episodeTitle", episodeTitle);
		rd.forward(request, response);   	
	}
	
	public void imgDelete(HttpServletRequest request, HttpServletResponse response, long cid, long eid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		String episodeTitle = "";
		Query query = pm.newQuery(Img.class);
		
		try{
			Comics comics = pm.getObjectById(Comics.class, cid);
			comicsTitle = comics.getTitle();
			
			Episode episode = pm.getObjectById(Episode.class, eid);
			episodeTitle = episode.getTitle();
			
			query.setOrdering("order desc");
			query.setFilter("eid == eidParam");
			query.declareParameters("Long eidParam");
			
			long iid = Long.valueOf(request.getParameter("iid"));
			Img img = pm.getObjectById(Img.class, iid);
			pm.deletePersistent(img);
			
			List<Img> imgList = (List<Img>)query.execute(eid);
			request.setAttribute("imgList", imgList);
			  
			RequestDispatcher rd = request.getRequestDispatcher("/manage/imgManage.jsp"); 
			request.setAttribute("cid", cid);
			request.setAttribute("eid", eid);
			request.setAttribute("comicsTitle", comicsTitle);
			request.setAttribute("episodeTitle", episodeTitle);
			rd.forward(request, response);   	
		}finally{
			query.closeAll(); 
			pm.close();   
		}
		
	}
	
	public void imgList(HttpServletRequest request, HttpServletResponse response, long cid, long eid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		String episodeTitle = "";
		Query query = pm.newQuery(Img.class);
		 
		try{
			Comics comics = pm.getObjectById(Comics.class, cid);
			comicsTitle = comics.getTitle();
			
			Episode episode = pm.getObjectById(Episode.class, Long.valueOf(eid));
			episodeTitle = episode.getTitle();
			
			query.setOrdering("order desc");
			query.setFilter("eid == eidParam");
			query.declareParameters("Long eidParam");
			  
			List<Img> imgList = (List<Img>)query.execute(eid);
			request.setAttribute("imgList", imgList);
			 
			RequestDispatcher rd = request.getRequestDispatcher("/manage/imgManage.jsp"); 
			request.setAttribute("cid", cid);
			request.setAttribute("eid", eid);
			request.setAttribute("comicsTitle", comicsTitle);
			request.setAttribute("episodeTitle", episodeTitle);
			rd.forward(request, response);   	
		}finally{
			query.closeAll(); 
			pm.close();  
		}
		
		
	}
}
